package com.villaday.idf.activity;

import android.graphics.Color;
import android.os.Build;
import androidx.annotation.RequiresApi;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;

import com.villaday.idf.R;
import com.villaday.idf.base.BaseNoWhiteActivity;
import com.villaday.idf.fragment.FodderFragmentFor3;
import com.villaday.idf.fragment.GoodsFragmentFor3;
import com.villaday.idf.imp.ChangeFaImp;
import com.villaday.idf.imp.GoodsDeticalChangeTitleImp;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.util.ImageViewUtils;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.StatusBarUtil;
import com.villaday.idf.util.TabLayoutUtils;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * 商品  素材
 *
 * */
public class GoodsAndFodderActivityFor3 extends BaseNoWhiteActivity implements GoodsFragmentFor3.ChangePage, GoodsDeticalChangeTitleImp, ChangeFaImp, GoodsFragmentFor3.ChangeTabPosition {

    @BindView(R.id.toolbar_iconBack)
    ImageButton toolbarIconBack;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.toolbar_iconNext)
    ImageButton toolbarIconNext;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.toolbar_fa)
    ImageButton toolbar_fa;
    @BindView(R.id.toolbar_icon)
    Toolbar toolbarIcon;

    private GoodsFragmentFor3 instance;
    private int scrollY;
    private int height;
    private View tabTagView;
    private float scale;

    @Override
    protected int getViewResId() {
        return R.layout.activity_goods_and_fodder;
    }

    @Override
    protected void init() {
        ImageViewUtils.setImageViewColor(toolbar_fa, Color.argb(255, 255, 255, 255));

        StatusBarUtil.setTranslucentForImageViewInFragment(this, null);
        StatusBarUtil.setLightMode(this);


        RefreshtokenEntity.BodyBean roleInfo =
                SharedPreferencesUtil.getRoleInfo(this);
        final String roleType = roleInfo.getMember().getType();
        int type = getIntent().getIntExtra("type", 0);

        final String persellId = getIntent().getStringExtra("persellId");
        final String houseId = getIntent().getStringExtra("houseId");
//右边图标
        toolbarIconNext.setVisibility(View.VISIBLE);
        //toolbarIconNext.setImageResource(R.drawable.detailspage_share);
        final String[] titles;
        if ("0".equals(roleType)) {
            if (SharedPreferencesUtil.getAgreementTuiJian(this)){
                titles = new String[]{"详情", "推荐"};

            }else {
                titles = new String[]{"详情"};

            }
            tab.setSelectedTabIndicatorHeight(0);
        } else {
            if (SharedPreferencesUtil.getAgreementTuiJian(this)){
                titles = new String[]{"详情", "推荐", "素材"};

            }else {
                titles = new String[]{"详情", "素材"};

            }
        }

        for (int i = 0; i < titles.length; i++) {
            tab.addTab(tab.newTab().setText(titles[i]));
        }
        instance = GoodsFragmentFor3.instance(houseId, persellId);
        instance.setChangeTabPosition(this);

        instance.setGoodsDeticalChangeTitleImp(this);
        instance.setChangeFaImp(this);
        instance.setChangePage(this);

        toolbar_fa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (instance != null) {
                    instance.xihuan();

                }
            }
        });
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    vp.setCurrentItem(0, true);
                    if (tabTagView != null && PhoneUtil.isVisible(GoodsAndFodderActivityFor3.this, tabTagView)) {
                        instance.setGoToTop();
                    }


                } else if (position == 1) {
                    vp.setCurrentItem(0, true);
                    instance.setGoToTuiJian();

                } else if (position == 2) {
                    vp.setCurrentItem(1, true);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    //LogeUtil.e("tabTagView",tabTagView.toString());

                    if (tabTagView != null && PhoneUtil.isVisible(GoodsAndFodderActivityFor3.this, tabTagView)) {
                        tab.getTabAt(1).select();
                        ;
                    } else {
                        tab.getTabAt(0).select();
                    }
                    // tab.getTabAt(0).select();

                } else {
                    tab.getTabAt(2).select();

                }
                if (position > 0) {


                    toolbarIcon.setBackgroundColor(Color.argb(255, 255, 255, 255));
                    tab.setTabTextColors(Color.argb(255, 50, 50, 50), Color.argb(255, 50, 50, 50));
                    tab.setSelectedTabIndicatorColor(Color.argb(255, 50, 50, 50));
                    ImageViewUtils.setImageViewColor(toolbarIconBack, Color.argb(255, 51, 51, 51));
                    ImageViewUtils.setImageViewColor(toolbarIconNext, Color.argb(255, 51, 51, 51));

                    //ImageViewUtils.setImageViewColor(toolbar_fa, Color.argb(255, 247, 54, 55));


                } else {
                    changeTitle(scrollY, height);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return position == 0 ? instance : FodderFragmentFor3.instance(persellId, houseId, getIntent().getLongExtra("brokerAge", 0));
            }

            @Override
            public int getCount() {
                return "0".equals(roleType) ? 1 : 2;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        //tab.setupWithViewPager(vp);
        vp.setCurrentItem(type);
        TabLayoutUtils.reflex(tab, 5);


    }


    @OnClick({R.id.toolbar_iconBack, R.id.toolbar_iconNext})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_iconBack: {//返回
                onBackPressed();
            }
            break;
            case R.id.toolbar_iconNext: {//右边图标
                if (instance != null) {
                    instance.shareXCX();
                }


            }
            break;
        }
    }


    @Override
    public void changePage() {
        vp.setCurrentItem(1);
    }

    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }

    @Override
    public void changeTitle(int scrollY, int height) {
        this.scrollY = scrollY;
        this.height = height - PhoneUtil.dp2px(this, 50);
        {
            if (scrollY <= 0) {
                //设置标题的背景颜色
                toolbarIcon.setBackgroundColor(Color.argb(0, 255, 255, 255));
                tab.setTabTextColors(Color.argb(0, 50, 50, 50), Color.argb(0, 50, 50, 50));
                tab.setSelectedTabIndicatorColor(Color.argb(0, 50, 50, 50));
                ImageViewUtils.setImageViewColor(toolbarIconBack, Color.argb(255, 255, 255, 255));
                ImageViewUtils.setImageViewColor(toolbarIconNext, Color.argb(255, 255, 255, 255));

                ImageViewUtils.setImageViewColor(toolbar_fa, Color.argb(255, 255, 255, 255));


                /* ivLeft.setAlpha(1f);
                ivRight.setAlpha(1f);
                ivLeft.setImageResource(R.mipmap.icon_back_goods);
                ivRight.setImageResource(mIsCollect ? R.mipmap.icon_collect_red1 : R.mipmap.icon_collect1);
*/
            } else if (scrollY > 1 && scrollY <= this.height) {
                // 滑动距离小于图的高度时，设置背景和字体颜色颜色透明度渐变
                scale = (float) scrollY / this.height;
                float alpha = (255 * scale);
                toolbarIcon.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                tab.setTabTextColors(Color.argb((int) alpha, 50, 50, 50), Color.argb((int) alpha, 50, 50, 50));
                tab.setSelectedTabIndicatorColor(Color.argb((int) alpha, 50, 50, 50));
                ImageViewUtils.setImageViewColor(toolbarIconBack, ImageViewUtils.blendColors(Color.argb(255, 255, 255, 255), Color.argb(255, 51, 51, 51), scale));
                ImageViewUtils.setImageViewColor(toolbarIconNext, ImageViewUtils.blendColors(Color.argb(255, 255, 255, 255), Color.argb(255, 51, 51, 51), scale));
                if (toolbar_fa.isSelected()) {
                    ImageViewUtils.setImageViewColor(toolbar_fa, ImageViewUtils.blendColors(Color.argb(255, 255, 255, 255), Color.argb(255, 247, 54, 55), scale));

                }else {
                    ImageViewUtils.setImageViewColor(toolbar_fa, ImageViewUtils.blendColors(Color.argb(255, 255, 255, 255), Color.argb(255, 51, 51, 51), scale));

                }


            } else {
                // 滑动到图下面设置普通颜色
                toolbarIcon.setBackgroundColor(Color.argb(255, 255, 255, 255));
                tab.setTabTextColors(Color.argb(255, 50, 50, 50), Color.argb(255, 50, 50, 50));
                tab.setSelectedTabIndicatorColor(Color.argb(255, 50, 50, 50));
                ImageViewUtils.setImageViewColor(toolbarIconBack, Color.argb(255, 51, 51, 51));
                ImageViewUtils.setImageViewColor(toolbarIconNext, Color.argb(255, 51, 51, 51));
                if (toolbar_fa.isSelected()) {
                    ImageViewUtils.setImageViewColor(toolbar_fa, Color.argb(255, 247, 54, 55));
                }else {
                    ImageViewUtils.setImageViewColor(toolbar_fa, Color.argb(255, 51, 51, 51));

                }

            }
        }
    }

    @Override
    public void onChangeFa(boolean isSelect) {
        toolbar_fa.setSelected(isSelect);
        if (toolbar_fa.isSelected()) {
            ImageViewUtils.setImageViewColor(toolbar_fa, ImageViewUtils.blendColors(Color.argb(255, 255, 255, 255), Color.argb(255, 247, 54, 55), scale));

        }else {
            ImageViewUtils.setImageViewColor(toolbar_fa, ImageViewUtils.blendColors(Color.argb(255, 255, 255, 255), Color.argb(255, 51, 51, 51), scale));

        }
    }

    @Override
    public void changeTabPosition(int position, View view) {
        try {
            tabTagView = view;
            int selectedTabPosition = tab.getSelectedTabPosition();
            if (selectedTabPosition != position) {
                tab.getTabAt(position).select();
            }
        } catch (Exception e) {

        }

    }
}
